<template>
  <el-dialog v-model="dialogInfo.visible" :title="dialogInfo.title" :width="dialogInfo.width" align-center>
    <el-form ref="formRef" label-position="left" :model="formModel" :rules="formRules" label-width="120px">
      <el-row>

        <!-- academyId: [{ required: true, trigger: "blur", message: "请选择校区名称" }],
  buildingId: [{ required: true, trigger: "blur", message: "请选择教学楼" }],
  name: [{ required: true, trigger: "blur", message: "请输入教室名称" }], -->
   <el-col :sm="24">
          <el-form-item label="教室名称" prop="name">
            <el-input clearable class="w100" v-model.trim="formModel.name" autocomplete="off" placeholder="请输入教室名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :sm="24">
          <el-form-item label="所属教学楼" prop="buildingId">
             <el-select clearable class="w100 mr-3" v-model="formModel.buildingId" placeholder="请选择教学楼" autocomplete="off">
              <el-option v-for="item in buildingOption" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :sm="24">
          <el-form-item label="录播主机IP" prop="recordingIp">
            <el-input clearable class="w100" v-model.trim="formModel.recordingIp" autocomplete="off" placeholder="请输入录播主机IP"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="info" plain @click="onCancel()">取消</el-button>
        <el-button type="primary" @click="onSubmit()">提交</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
/**
 * 管理员编辑组件
 */
import { ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import EsAjax from '@/utils/EsAjax';
import ApiUrls from '@/apis/index';
import { getNewObj } from '@/utils/EsCommon';
import { courseParams, degreeTypeList, courseTypeList, classList } from '@/apis/model/CourseModel';




// ======================================================
//                      变量声明
// ======================================================

// emit
const emit = defineEmits(['success']);
// 表单数据
const formAttrs = {
  id: null, // 教室ID
  name: '',//教室名称
  // campusId: null, // 校区名称
  buildingId: null, // 教学楼
  recordingIp: '', //录播主机 IP

};

const formModel = ref(Object.assign({}, formAttrs));
// 弹窗信息
const dialogInfo = ref({
  visible: false,
  title: '',
  width: '650px',
})
// 表单组件的引用实例
const formRef = ref(null);


const campusOption = ref([]);
const buildingOption = ref([]);
const academyOption = ref([]);
const majorOption = ref([]);
// 表单校验规则
const formRules = ref({
  recordingIp: [{ required: true, trigger: "blur", message: "请输入录播主机IP" }],
  buildingId: [{ required: true, trigger: "blur", message: "请选择教学楼" }],
  name: [{ required: true, trigger: "blur", message: "请输入教室名称" }],
})

// ======================================================
//                      点击操作
// ======================================================

// 点击提交按钮
const onSubmit = () => {
  formRef.value.validate((valid, fields) => {
    if (!valid) {
      console.log('表单校验失败：', fields);
      return;
    }
    let url = formModel.value.id > 0 ? ApiUrls.roomChange : ApiUrls.roomAdd;
    EsAjax.post(url, formModel.value).then(res => {
      if (res.data.code !== 200) {
        ElMessage.warning(res.data.msg);
        return;
      }
      dialogInfo.value.visible = false;
      ElMessage.success('提交成功');
      emit('success');
    }).catch(err => {
      ElMessage.error('网络错误：' + err.message);
    })
  })
}

// 点击取消按钮
const onCancel = () => {
  formModel.value = Object.assign({}, formAttrs);
  formRef.value.resetFields();
  dialogInfo.value.visible = false;
}
// ======================================================
//                      处理事件
// ======================================================

// 加载数据
const show = async (id, title, item = null, width = '650px') => {
  dialogInfo.value.title = title;
  dialogInfo.value.width = width;
  dialogInfo.value.visible = true;
  formModel.value = Object.assign({}, formAttrs)
  // 加载详情数据
  if (id) {
    const _form = getNewObj(item)
    formModel.value = Object.assign({ ...formAttrs }, {
      id:_form.id,
      name:_form.Name,
      buildingId:_form.Building.id,
      recordingIp:_form.recordingIp,

    });
  }
  // getAcademyList();
  getBuildingList();
  // getCampusList();
}
const getCampusList = () => {
  EsAjax.post(ApiUrls.campusList, { }).then(res => {
    console.log(res)
    if (res.data.code !== 200) {
      ElMessage.warning(res.data.msg);
      return;
    }
    let { list } = res.data.data
    campusOption.value = list;
  }).catch(err => {
    ElMessage.error(err.message);
  })
}
const getBuildingList = () => {
  EsAjax.post(ApiUrls.buildingList, { }).then(res => {
    console.log(res)

    if (res.data.code !== 200) {
      ElMessage.warning(res.data.msg);
      return;
    }
    let { list } = res.data.data
    buildingOption.value = list;
  }).catch(err => {
    ElMessage.error(err.message);
  })
}

const getAcademyList = () => {
  EsAjax.post(ApiUrls.academyList, {  }).then(res => {
    console.log(res)

    if (res.data.code !== 200) {
      ElMessage.warning(res.data.msg);
      return;
    }
    let { list } = res.data.data
    academyOption.value = list;
  }).catch(err => {
    ElMessage.error(err.message);
  })
}

defineExpose({
  show
})
</script>

<style scoped>
</style>
